﻿using DDDDemoABP1.DAL;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ObjectMapping;

namespace DDDDemoABP1.Payments;

public class DALAccountRepository : IAccountRepository, ITransientDependency
{
    private readonly AccountDAL _AccountDAL;
    private readonly IObjectMapper _ObjectMapper;

    public DALAccountRepository(AccountDAL accountDAL, IObjectMapper objectMapper)
    {
        _AccountDAL = accountDAL;
        _ObjectMapper = objectMapper;
    }

    public async Task AddAsync(Account account)
    {
        var entity = _ObjectMapper.Map<Account, AccountDALModel>(account);
        await _AccountDAL.AddAsync(entity);
    }

    public async Task DeleteAsync(Guid id)
    {
        await _AccountDAL.DeleteAsync(id);
    }

    public async Task<Account> FindAsync(Guid id)
    {
        var data = await _AccountDAL.GetAccountByIdAsync(id);
        if (data == null)
        {
            return null;
        }
        var result = new Account(data.UserId, data.AccountNo, data.Balance);
        return result;
    }

    public async Task<Account> FindAsync(string accountNo)
    {
        var data = await _AccountDAL.GetAccountByNoAsync(accountNo);
        if (data == null)
        {
            return null;
        }
        var result = new Account(data.UserId, data.AccountNo, data.Balance);
        return result;
    }

    public async Task UpdateAsync(Account account)
    {
        var entity = _ObjectMapper.Map<Account, AccountDALModel>(account);
        await _AccountDAL.UpdateAsync(entity);
    }
}